<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 账户异动ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */

quanli_require('module/base/customer/logHandler.class.php');
quanli_require('module/base/customer/financeChange.class.php');
quanli_require('module/func/eai.func.php');
quanli_require('quanliadmin/comm/exportExcel_fun.php');

class ajax_financeChange {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //账户异动查询
                    $this->accountSelect();
                    break;

                case 2: //编辑异动账户信息查询
                    $this->searchCustomerData();
                    break;

                case 5: //导出
                    $this->excelout();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'change'))) {
                        returnJsonData('参数错误');
                    }
                    if($_REQUEST['act'] == 'edit'){

                        $this->accountSave();//新增、修改账户异动

                    }

                    break;

                case 101:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('audit', 'edit'))) {
                        returnJsonData('参数错误');
                    }
                    if($_REQUEST['act'] == 'audit'){

                        $this->accountAudit();//账户异动审核或过账

                    }

                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    /**
     * 账户异动查询
     */
    private function accountSelect() {

        global $G_PGLOB;

        //实例化会员类
        $financeChange = new financeChange( $G_PGLOB->DBCA );

        //查询会员账户异动信息
        $list = $financeChange->searchFinanceInfo( $_REQUEST );
        if ($_REQUEST['type'] == 2){
            $result_array = $list['money'];
        }else{
            $account_array = $list['account_list'];
            //付款方式
            $itemHandler = new itemHandler();
            $args_array = array( 'item' => '43');
            $item_category_array = $itemHandler->searchItemList($args_array);
            foreach($item_category_array as &$item) {
                $item_category[$item->itemID] = $item->itemName;
            }
            //组织返回的数据
            $result_array = array(
                'page' => $_REQUEST['page'],
                'total' => $_REQUEST['count'],
                'rows' => array()
            );

            if ($account_array) {

                $edit_permission = $G_PGLOB->has_permission(3006);

                foreach($account_array as &$account) {

                    if($account['changeAmount'] > 0 ){
                        $changeStatusName = '应收';
                    }else{
                        $changeStatusName = '实收';
                    }

                    if($edit_permission){
                        $result_array['rows'][] = array(
                            'id' => $account['changeID'],
                            'cell' => array(
                                $account['changeID'],
                                $account['custName'],
                                $account['saleCode'],
                                $item_category[$account['paymentMethod']],
                                $account['changeAmount'],
                                $account['createTime'],
                                $changeStatusName,
                                $account['remark']
                            )
                        );
                    }else{
                        $result_array['rows'][] = array(
                            'id' => $account['changeID'],
                            'cell' => array(
                                $account['changeID'],
                                $account['custName'],
                                $account['saleCode'],
                                $item_category[$account['paymentMethod']],
                                $account['changeAmount'],
                                $account['createTime'],
                                $changeStatusName,
                                $account['remark']
                            )
                        );
                    }
                }
            }
        }
        echo json_encode($result_array);

    }

    /**
     * 编辑异动账户信息查询
     */
    private function searchCustomerData() {

        global $G_PGLOB;

        if(isset($_REQUEST['changeID']) && !empty($_REQUEST['changeID'])){
            //实例化会员类
            $financeChange = new financeChange( $G_PGLOB->DBCA );

            //查询会员账户异动信息
            $account_array = $financeChange->searchFinanceInfo( $_REQUEST );

            echo json_encode(array('code'=>200 , 'data'=>$account_array[0]));
        }else{
            echo json_encode(array('code'=>400 , 'message'=>'异动账户信息没找到，请刷新页面重试'));
        }


    }

    /**
     * 新增账户异动信息
     */
    private function accountSave() {
        global $G_PGLOB;

        $financeChange = new financeChange( $G_PGLOB->DBCA );

        $_REQUEST['creator'] = $G_PGLOB->currentUserName;

        $result_array = $financeChange->saveAccountChange( $_REQUEST);

        echo json_encode($result_array);
    }

    public function excelout(){
        global $G_PGLOB;

        //实例化会员类
        $financeChange = new financeChange( $G_PGLOB->DBCA );

        //查询会员账户异动信息
        $account_array = $financeChange->searchFinanceInfo( $_REQUEST );
        //付款方式
        $itemHandler = new itemHandler();
        $args_array = array( 'item' => '43');
        $item_category_array = $itemHandler->searchItemList($args_array);
        foreach($item_category_array as &$item) {
            $item_category[$item->itemID] = $item->itemName;
        }
        if ($account_array['account_list']) {

            foreach($account_array['account_list'] as &$account) {

                if($account['changeAmount'] > 0 ){
                    $changeStatusName = '应收';
                }else{
                    $changeStatusName = '实收';
                }
                $data[] = array(
                    'changeID' => $account['changeID'],
                    'custName' => $account['custName'],
                    'saleCode' => $account['saleCode'],
                    'paymentMethod' => $item_category[$account['paymentMethod']],
                    'changeAmount' => $account['changeAmount'],
                    'createTime' => $account['createTime'],
                    'changeStatusName' => $changeStatusName,
                    'remark' => $account['remark']
                );
            }
        }
        $xlsName="账户异动列表";//文件名
        $xlsCell=array(
            array('changeID','流水号'),
            array('custName','客户名称'),
            array('saleCode','订单号'),
            array('paymentMethod','支付方式'),
            array('changeAmount','异动金额'),
            array('createTime','创建时间'),//
            array('changeStatusName','异动状态'),
            array('remark','备注'),

        );
        exportExcel($xlsName,$xlsCell,$data);

    }
}